<template>
    <div>
        <el-row :gutter="18">
            <el-col :span="20" >
                <div style="margin-left:20px;font-weight: bolder;color:#666">添加文章</div>
            </el-col>

            <el-col :span="4">
                <el-button @click="go">返回列表</el-button>
            </el-col>
        </el-row>

        <el-form  ref="form" :model="form" label-position="labelPosition" label-width="100px">
            <el-form-item label="选择栏目" >
                <!--<el-select >-->
                    <!--<el-option :label="1" :value="1" ></el-option>-->
                    <!--<el-option :label="1" :value="1" ></el-option>-->
                <!--</el-select>-->
                <el-select v-model="form.article_programa_id" placeholder="请选择">
                    <el-option
                            v-for="item in options2"
                            :key="item.id"
                            :label="item.value"
                            :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="文章标题" >
                <el-input style="width:300px" v-model="form.title"></el-input>
            </el-form-item>

            <el-form-item label="副标题" >
                <el-input style="width:300px" v-model="form.subtitle"></el-input>
            </el-form-item>
            <el-form-item label="文章摘要" >
                <el-input
                        type="textarea"
                        :rows="3"
                        placeholder="文章摘要"
                        v-model="form.abstracts" style="width:300px">
                </el-input>
            </el-form-item>
            <el-form-item label="来源" >
                <el-input style="width:300px" v-model="form.source"></el-input>
            </el-form-item>
        </el-form>
        <div style="margin-bottom:20px;margin-left:45px;font-size:16px;color:#666">缩略图</div>
        <div style="margin-left:100px">
            <el-upload
                    class="avatar-uploader"
                    :action=fileUploadAction
                    :show-file-list="false"
                    :on-error="err"
                    :on-success="handleAvatarSuccess"
                    :before-upload="beforeAvatarUpload">
                <img v-if="imageUrl" :src=imageUrl class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                <div slot="tip" class="el-upload__tip">（支持png/jpg格式，文件大小不能超过3M）</div>
            </el-upload>
        </div>
        <div style="margin-bottom:20px;margin-left:45px;font-size:16px;color:#666">
            附件
        </div>
        <div style="margin-left:100px;margin-bottom:20px">
            <el-upload
                    class="upload-demo"
                    :action=fileUploadAction
                    :on-remove="remove"
                    :on-success="success"
                    :on-error="err"
                    :before-upload="beforeAvatarUpload1"
                    :on-change="handleChange"
                    :file-list="fileList3">
                <el-button size="small" type="primary">点击上传</el-button>
                <div slot="tip" class="el-upload__tip">（支持word/excle/pdf格式，文件大小不能超过50M</div>
            </el-upload>

        </div>
        <el-form  label-position="labelPosition" label-width="100px">

            <el-form-item label="可读权限" >
                <template>
                    <el-radio class="radio" v-model="form.readability" label="1">游客</el-radio>
                    <el-radio class="radio" v-model="form.readability" label="2">会员</el-radio>
                </template>
            </el-form-item>

            <el-form-item label="视频地址" >
                <el-input style="width:300px" v-model="form.video"></el-input>
            </el-form-item>
            <el-form-item label="文章内容" >
                <!--<script id="editor" type="text/plain"></script>-->
                <div style="max-width:1200px">
                    <!--<script id="editor" type="text/plain"></script>-->
                    <script id="editor" type="text/plain"></script>
                </div>
            </el-form-item>
            <el-form-item label="Keywords" >
                <el-input style="width:300px" v-model="form.keywords"></el-input>
            </el-form-item>
            <el-form-item label="Description" >
                <el-input style="width:300px" v-model="form.description"></el-input>
            </el-form-item>
        </el-form>
        <div style="width:200px;margin:50px auto;"><el-button  type="primary" style="width: 200px" @click="submit">发布</el-button></div>
 </div>
</template>
<script>
    import _http from '@/components/common/http.vue'
    import _param from '@/components/common/param.vue'
    import '@/assets/ueditor/ueditor.config.js'
    import '@/assets/ueditor/ueditor.all.min.js'
    import '@/assets/ueditor/ueditor.parse.min.js'
    import '@/assets/ueditor/lang/zh-cn/zh-cn.js'
    export default {
        data() {
            return {
                options2:[],
                id:'',
                fileList3:[],
                imageUrl: '',
                apiPort:{
///                    发布内容接口
                    fabuList:"article/addArticle",
//                    回显接口
                    getBack:"article/findById",
//                    编辑上传接口
                    editUp:'article/updateArticle',
//                    栏目下拉
                    lanM:'article/findPrograma'
                },
                editor:null,
                fileUploadAction:_param.param.fileUploadUrl,
                userid:'',
                textarea:'',
                form:{
                    title:'',
                    mainbody:'',
                    userid:'',
                    abstracts:'',
                    article_programa_id:'',
                    subtitle:'',
                    source:'',
                    readability:'',
                    video:'',
                    keywords:'',
                    description:'',
                    picture:'',
                    attachment:''
                },
                activeName: 'second',
                labelPosition: 'right',
                dialogImageUrl: '',
                dialogVisible: false
            };
        },
        mounted(){
            this.userid=localStorage.getItem('userId')
            this.form.userid = localStorage.getItem('userId')
            this.id = this.$route.query.info
            this.getsele()
            var that = this

            this.editor = UE.getEditor('editor');
            if(this.id!='n'){
                console.log(this.id)
                this.getBack(this.id)
            }
        },
        destroyed() {
            this.editor.destroy();
        },
        methods: {
            getsele(){
                var that = this
                var params = that.querystring.stringify({userid:that.userid})
                _http.axiosRequest(that,that.apiPort.lanM,params,(res)=>{
                    console.log(res.data.data.filemanage)
                that.options2=res.data.data.articlePrograma
            })
            },
            addAr(){
                this.form.mainbody=this.editor.getContent()
                var that = this
                var params = that.querystring.stringify(this.form)
                console.log(this.form)
                _http.axiosRequest(that,that.apiPort.fabuList,params,(res)=>{
                    console.log(res)
                    if(res.data.code==100){
                    that.$message({
                        message: '文章新增成功',
                        type: 'success'
                    });
                    setTimeout(function(){
                        that.$router.go(-1)
                    },500)
                }else{
                    that.$message({
                        message:res.data.msg,
                        type: 'warning'
                    });
                }
            })
            },
            editAr(){
                this.form.mainbody=this.editor.getContent()
                var that = this
                this.form.id=this.id
                var params = that.querystring.stringify(this.form)
                console.log(23456789)
                console.log(this.form)
                _http.axiosRequest(that,that.apiPort.editUp,params,(res)=>{
                    console.log(res)
                if(res.data.code==100){
                    that.$message({
                        message: '文章编辑成功',
                        type: 'success'
                    });
                    setTimeout(function(){
                        that.$router.go(-1)
                    },500)
                }else{
                    that.$message({
                        message:res.data.msg,
                        type: 'warning'
                    });
                }
            })
            },
            //            获取回显内容
            getBack(id){
                var that = this
                var params = that.querystring.stringify({'id':id,'userid':this.form.userid})
                _http.axiosRequest(that,that.apiPort.getBack,params,(res)=>{
                    console.log(res)
                that.form.title = res.data.data.title
                that.form.abstracts=res.data.data.abstracts
                that.form.article_programa_id=res.data.data.article_programa_id
                that.form.subtitle=res.data.data.subtitle
                that.form.source=res.data.data.source
                that.form.readability=res.data.data.readability+''
                that.form.video=res.data.data.video
                that.form.keywords=res.data.data.keywords
                that.form.description=res.data.data.description
                that.editor.setContent(res.data.data.mainbody)
                that.imageUrl = res.data.data.picture
                that.form.picture=res.data.data.picture
                that.form.attachment=res.data.data.attachment
                if(res.data.data.attachment!=''||null){
                    that.fileList3=[{name:'附件',url:res.data.data.attachment}]
                }
            })
            },
            //            发布提交
            submit(){
                if(this.id!='n'){
                    this.editAr()
                }else{
                    this.addAr()
                }
            },
            handleClick(tab, event) {
                console.log(tab, event);
            },
            go(){
//                router.go({name: 'fabu', params: {userId: 1}});
                this.$router.go(-1)
            },
            handleRemove(file, fileList) {
                console.log(file);
                console.log(fileList);
            },
            handlePictureCardPreview(file,fileList) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
                console.log(file.response)
            },
            success(response,file,fileList){
                console.log(response)
                console.log(file.url)
                this.form.attachment=response.url
                console.log(fileList)
            },
            remove(file,fileList){
                console.log('remove')
                console.log(fileList)
                this.fileList3=[]
                this.form.attachment=''

            },
            handleAvatarSuccess(res, file) {
                this.imageUrl = URL.createObjectURL(file.raw);
                console.log(file.url)
                this.form.picture=res.url
            },
            beforeAvatarUpload(file) {
//                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 3;

                if (!(file.type == 'image/jpeg'||file.type == 'image/png')) {
                    this.$message.error('图片只能是 JPG或者PNG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('图片大小不能超过 3MB!');
                }
                return isLt2M&&(file.type == 'image/jpeg'||file.type == 'image/png');
            },
            handleChange(file, fileList) {
                this.fileList3 = fileList.slice(-1);
//                this.form.attachment=file.url
            },
            err(){
                this.$message.error('上传失败');
            },
            beforeAvatarUpload1(file) {
//                application/vnd.openxmlformats-officedocument.wordprocessingml.document
//                application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
//                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 50;
                console.log(file.type)

                if (!(file.type == 'application/pdf'||file.type == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'||file.type == 'application/vnd.openxmlformats-officedocument.wordprocessingml.document')) {
                    this.$message.error('上传文件只能是word/excle/pdf 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传文件大小不能超过 50MB!');
                }
                return isLt2M&&(file.type == 'application/pdf'||file.type == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'||file.type == 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
            }
        }
    };
</script>
<style>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #20a0ff;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }
    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
</style>